package org.luxor.commons.workflow.entity;

import org.apache.commons.lang.StringUtils;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 发起流程
 *
 * @author Mr.yan  @date 2021/10/17
 */
public class StartProcess {

    /**
     * 租户ID
     */
    private String tenantId;

    /**
     * 流程定义ID
     */
    @NotBlank(message = "流程定义ID(processDefinitionId),不允许为空")
    private String processDefinitionId;

    /**
     * 流程实例名称
     */
    @NotBlank(message = "流程实例名称(name),不允许为空")
    private String name;

    /**
     * 发起人
     */
    @NotBlank(message = "发起人(startUserId),不允许为空")
    private String startUserId;

    /**
     * 业务编码(一般为业务表单的唯一编码)
     */
    private String businessKey;

    /**
     * 目标任务-受理人
     */
    private String assignee;

    /**
     * 目标任务-候选人
     */
    private Candidate candidate;

    /**
     * 目标任务-到期时间
     */
    private Date dueDate;

    /**
     * 目标任务-优先级
     */
    private Integer priority;

    /**
     * 流程变量
     */
    private Map<String, Object> variables;

    public StartProcess() {
        this.candidate = new Candidate();
        this.variables = new HashMap<>();
    }

    public StartProcess(String processDefinitionId) {
        this();
        this.processDefinitionId = processDefinitionId;
    }

    public StartProcess(String processDefinitionId, String startUserId, String candidateUser) {
        this(processDefinitionId);
        this.startUserId = startUserId;
        this.addCandidateUser(candidateUser);
    }

    public String getProcessDefinitionId() {
        return processDefinitionId;
    }

    public void setProcessDefinitionId(String processDefinitionId) {
        this.processDefinitionId = processDefinitionId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStartUserId() {
        return startUserId;
    }

    public void setStartUserId(String startUserId) {
        this.startUserId = startUserId;
    }

    public String getBusinessKey() {
        return businessKey;
    }

    public void setBusinessKey(String businessKey) {
        this.businessKey = businessKey;
    }

    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public String getAssignee() {
        return assignee;
    }

    public void setAssignee(String targetAssignee) {
        this.assignee = targetAssignee;
    }

    public Candidate getCandidate() {
        return candidate;
    }

    public void setCandidate(Candidate candidate) {
        if (candidate == null) {
            this.candidate = new Candidate();
        } else {
            this.candidate = candidate;
        }
    }

    public void addCandidateUser(String userId) {
        this.candidate.addUser(userId);
    }

    public void addCandidateGroup(String groupId) {
        this.candidate.addGroup(groupId);
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Map<String, Object> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, Object> variables) {
        if (variables == null) {
            this.variables = new HashMap<>();
        } else {
            this.variables = variables;
        }
    }

    public void addVariable(String key, Object value) {
        if (StringUtils.isNotBlank(key)) {
            this.variables.put(key, value);
        }
    }

    public void removeVariable(String key) {
        if (StringUtils.isNotBlank(key)) {
            this.variables.remove(key);
        }
    }

    @Override
    public String toString() {
        return "StartProcess{" +
                "tenantId='" + tenantId + '\'' +
                ", processDefinitionId='" + processDefinitionId + '\'' +
                ", name='" + name + '\'' +
                ", startUserId='" + startUserId + '\'' +
                ", businessKey='" + businessKey + '\'' +
                ", assignee='" + assignee + '\'' +
                ", candidate=" + candidate +
                ", dueDate='" + dueDate + '\'' +
                ", priority=" + priority +
                ", variables=" + variables +
                '}';
    }
}
